home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5831 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  93 lines

  1. Path: drivel.ics.uci.edu!ucivax!gateway
  2. From: klefstad@catalina.ICS.UCI.EDU ("Raymond Klefstad, Ph.D.")
  3. Subject: Composite functions in C++ - an example
  4. Message-ID: <9602061650.aa09526@paris.ics.uci.edu>
  5. Newsgroups: comp.lang.c++,ics.141,ics.145a,ics.147
  6. Date: 7 Feb 96 00:54:45 GMT
  7.  
  8.  
  9.  
  10. /*
  11.     A collegue of mine who does research in Machine Learning asked me
  12.     how to do composition of functions in C++.  My solution uses a
  13.     combination of operator overloading, pure virtual functions, and
  14.     inheritance.  I thought other C++ users might find it interesting.
  15.     The domain and range must be the same for these functions for
  16.     composition to make sense.  This particular program composes
  17.     functions mapping double into double, but you could modify it
  18.     for other types including mapping lists into lists for LISP-like
  19.     composition.  Check out the polymorphic integrate function used
  20.     as an example user of functions.
  21.  
  22.     Raymond Klefstad, Ph.D.   U.C. Irvine,  2/6/96
  23. */
  24.  
  25. #include <iostream.h>
  26.  
  27.  
  28. typedef double (*doublefn)(double);
  29.  
  30. struct function
  31. {
  32.     virtual double operator () (double d) = 0;
  33. };
  34.  
  35. struct primitive : function
  36. {
  37.     doublefn fp;
  38.     primitive(doublefn f) : fp(f) {}
  39.     double operator () (double d) {return fp(d);}
  40. };
  41.  
  42. struct composite : function
  43. {
  44.     function &fp, &gp;
  45.     composite(function &f, function &g) : fp(f), gp(g) {}
  46.     double operator () (double d) {return fp(gp(d));}
  47. };
  48.  
  49.  
  50. // Approximate an integral of f from lower to upper
  51.  
  52. double integrate(function &f, double lower, double upper)
  53. {
  54.     double step = (upper-lower)/100000;
  55.     double sum = 0;
  56.     for (double x = lower; x <= upper; x += step)
  57.         sum += f(x);
  58.     return sum * step;
  59. }
  60.  
  61.  
  62. // Some test functions
  63.  
  64. double f(double d)
  65. {
  66.     return d*d;
  67. }
  68.  
  69. double g(double d)
  70. {
  71.     return d+2;
  72. }
  73.  
  74.  
  75. void main()
  76. {
  77.     const double Low = 0.0;
  78.     const double High = 2.0;
  79.  
  80.     primitive f1 = f, g1 = g;
  81.     composite fg(f1, g1);
  82.     composite gf(g1, f1);
  83.     composite fggf(fg, gf);
  84.     composite gffg(gf, fg);
  85.  
  86.     cout << integrate(f1, Low, High) << endl;
  87.     cout << integrate(g1, Low, High) << endl;
  88.     cout << integrate(fg, Low, High) << endl;
  89.     cout << integrate(gf, Low, High) << endl;
  90.     cout << integrate(fggf, Low, High) << endl;
  91.     cout << integrate(gffg, Low, High) << endl;
  92. }
  93.